/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.garcomeletronico.dao.implementacao;

import br.com.garcomeletronico.bean.Funcionario;
import br.com.garcomeletronico.dao.IDAOFuncionario;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Rivi
 */
public class DAOFuncionario extends DAOGenerico implements IDAOFuncionario {

    @Override
    public void inserir(Funcionario c) throws SQLException {

        try {
            String sql = "insert into Funcionario (cpf, nome, cargo) "
                    + " values (?,?,?)";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setString(1, c.getCpf());
            stmt.setString(2, c.getNome());
            stmt.setString(3, c.getCargo());

            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

    }

    @Override
    public void alterar(Funcionario c) throws SQLException {

        try {
            String sql = ("update Funcionario set cpf= ?, nome= ?, cargo= ? "
                    + "where codigo= ? ");
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setString(1, c.getCpf());
            stmt.setString(2, c.getNome());
            stmt.setString(3, c.getCargo());
            stmt.setInt(4, c.getCodigo());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }

    @Override
    public void excluir(Funcionario c) throws SQLException {
        try {
            String sql = "delete from Funcionario where codigo=?";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, c.getCodigo());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }

    @Override
    public List<Funcionario> listarPorNome(Funcionario c) throws SQLException {

        String sql = "select * from Funcionario where nome like '%" + c.getNome() + "%'";
        List<Funcionario> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }

                Funcionario c1 = new Funcionario();
                c1.setCodigo(rs.getInt("codigo"));
                c1.setCpf(rs.getString("cpf"));
                c1.setNome(rs.getString("nome"));
                c1.setCargo(rs.getString("cargo"));
                minhaLista.add(c1);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return minhaLista;

    }

    @Override
    public List<Funcionario> listarTodos() throws SQLException {
        String sql = "select * from Funcionario";
        List<Funcionario> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }

                Funcionario c1 = new Funcionario();
                c1.setCodigo(rs.getInt("codigo"));
                c1.setCpf(rs.getString("cpf"));
                c1.setNome(rs.getString("nome"));
                c1.setCargo(rs.getString("cargo"));
                minhaLista.add(c1);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return minhaLista;
    }

    public boolean consultar(Funcionario c) throws SQLException {
        boolean autenticado = false;
        String sql = "select nome, cpf from Funcionario where nome = ? and cpf = ?";
        PreparedStatement stmt = this.getConexao().prepareStatement(sql);
        stmt.setString(2, c.getNome());
        stmt.setString(1, c.getCpf());
        ResultSet rs;
        rs = stmt.executeQuery();

        if (rs.next()) {
            String loginNome = rs.getString("NOME_DA_COLUNA_QUE_TEM_O_LOGIN");
            String senhaCpf = rs.getString("NOME_DA_COLUNA_QUE_TEM_A_SENHA");
            autenticado = true;
        }
        rs.close();
        stmt.close();
        return autenticado;

    }

    @Override
    public Funcionario listarPorId(Funcionario pojo) throws SQLException {
        return null;
    }
}
